include('shared.lua')

local HoverTex=Material("effects/blueflare1")

local EnterMessageAlpha=0
LocalPlayerIsDrivingShuttle=false

function ShuttleLocalDrivingChanged(entity, name, oldval, newval)
	if newval==true and LocalPlayerIsDrivingShuttle==false then
		EnterMessageAlpha=4
	end
	LocalPlayerIsDrivingShuttle=newval
end
hook.Add("InitPostEntity","ShuttleInitAddProxy",function()
	LocalPlayer():SetNetworkedVarProxy( "isDriveShuttle", ShuttleLocalDrivingChanged )
end)

local ShuttleInstructions={
	"You're in the shuttle!"
}

function ShuttleDrawInfoOverlay()
	if EnterMessageAlpha>0 then
		EnterMessageAlpha=EnterMessageAlpha-0.01
		local tmsgA=math.Clamp(EnterMessageAlpha,0,1)
		for k,v in pairs(ShuttleInstructions) do
			local individualsize=25
			local totalsize=#ShuttleInstructions*individualsize
			draw.WordBox(5, 50, (ScrH()/2)-(totalsize/2)+(individualsize*k), v, "Default", Color(200,150,0,128*tmsgA), Color(0,0,0,255*tmsgA) )
		end
	end
end
hook.Add("HUDPaint","ShuttleDrawInfoOverlay",ShuttleDrawInfoOverlay)

function ENT:Draw()
	self.Entity:DrawModel()
	--if ( LocalPlayer():GetEyeTrace().Entity == self.Entity && EyePos():Distance( self.Entity:GetPos() ) < 512 ) then
		--AddWorldTip(self.Entity:EntIndex(),"Health: "..tostring(self.Entity:GetNetworkedInt("health",0)),0.5,self.Entity:GetPos(),self.Entity)
	--end
	if self.Entity:GetNetworkedBool("hover",false) then
		local size=math.abs(math.Clamp(math.sin(CurTime()*2),0,1)*300)
		local alph=math.abs(math.Clamp(math.sin(CurTime()),0,1))
		render.SetMaterial( HoverTex )
		render.DrawSprite(self.Entity:GetPos(), size, size, Color(50,50,200,alph))
	end
end

function sCalc( ply, origin, angles, fov )
	if not LocalPlayer().sDistz then
		LocalPlayer().sDistz=100
	end
	if LocalPlayer().sDistz<1 then LocalPlayer().sDistz=1 end
	if LocalPlayer().sDistz>1000 then LocalPlayer().sDistz=1000 end
	local shut=LocalPlayer():GetNetworkedEntity("Shuttle",LocalPlayer())
	if LocalPlayerIsDrivingShuttle and shut~=LocalPlayer() and shut:IsValid() then
		local view = {}
			view.origin = shut:GetPos()+ply:GetAimVector():GetNormal()*-LocalPlayer().sDistz
			view.angles = angles
		return view
	end
end
hook.Add("CalcView", "MyCalcView", sCalc)

function ENT:Think()	
	if LocalPlayerIsDrivingShuttle then
		if LocalPlayer():KeyDown(IN_JUMP) then
			LocalPlayer().sDistz=LocalPlayer().sDistz+5
		end
		if LocalPlayer():KeyDown(IN_DUCK) then
			LocalPlayer().sDistz=LocalPlayer().sDistz-5
		end
	end
end